package jmine.tec.purge.scheduling;

import jmine.tec.purge.PurgeMessages;
import jmine.tec.purge.dao.PurgeConfigDAO;
import jmine.tec.purge.domain.PurgeConfig;
import jmine.tec.purge.domain.PurgeConfigIdentifier;
import jmine.tec.scheduling.ScheduledJobExecutionContext;
import jmine.tec.scheduling.condition.Condition;

/**
 * Scheduling condition that checks if a purge must be executed
 * 
 * @author lundberg
 */
public class PurgeCondition implements Condition {

    private final PurgeConfigDAO purgeConfigDAO;

    private final PurgeConfigIdentifier purgeConfigIdentifier;

    /**
     * @param purgeConfigDAO purgeConfigDAO
     * @param purgeConfigIdentifier purgeConfigIdentifier
     */
    public PurgeCondition(PurgeConfigDAO purgeConfigDAO, PurgeConfigIdentifier purgeConfigIdentifier) {
        this.purgeConfigDAO = purgeConfigDAO;
        this.purgeConfigIdentifier = purgeConfigIdentifier;
    }

    /**
     * {@inheritDoc}
     */
    public boolean isSatisfied(ScheduledJobExecutionContext context) {
        PurgeConfig config = this.purgeConfigDAO.findBySystemIdentifier(this.purgeConfigIdentifier);
        context.log(PurgeMessages.STARTING_SCHEDULED_PURGE.create(config.getName()));
        if (config.isEnabled()) {
            context.log(PurgeMessages.SCHEDULED_PURGE_ENABLED.create());
            return true;
        } else {
            context.log(PurgeMessages.SCHEDULED_PURGE_DISABLED.create());
            return false;
        }
    }
}
